using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.Repositories
{
	#region using
	using AppManagement.Models;
	using AppManagement.Helpers;
	#endregion
	public class NHACUNGCAPRepository
	{
		public static List<NHACUNGCAP> SelectAll()
		{
			try
			{
				return DataContext.Instance.NHACUNGCAP.ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<NHACUNGCAP>();
		}

		public static NHACUNGCAP SelectByID(int ncc_id)
		{
			try
			{
				return DataContext.Instance.NHACUNGCAP.FirstOrDefault(item => item.NCC_ID == ncc_id );
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return null;
		}

		public static bool Insert(NHACUNGCAP obj)
		{
			try
			{
				DataContext.Instance.NHACUNGCAP.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Delete(int ncc_id)
		{
			try
			{
				var delitem = DataContext.Instance.NHACUNGCAP.FirstOrDefault(item => item.NCC_ID == ncc_id );
				DataContext.Instance.NHACUNGCAP.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
				return false;
		}

		public static List<NHACUNGCAP> RetrieveByID(int ncc_id)
		{
			try
			{
				return (from item in DataContext.Instance.NHACUNGCAP where  item.NCC_ID == ncc_id  select item).ToList();
			}
			catch(Exception ex)
			{
				if (ex.InnerException != null)
				{
					GlobalVariables.Logger.Error(ex.InnerException.Message);
				}
				else
				{
					GlobalVariables.Logger.Error(ex.Message);
				}
			}
			return new List<NHACUNGCAP>();
		}

	}
}